package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    /**
     * 发布视频
     *  接口路径：POST
     *  请求参数：
     *      videoThumbnail：封面图
     *      videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }
    /**
     * 视频点赞
     */
    @PostMapping("/{id}/like")
    public ResponseEntity videoLike(@PathVariable("id")String videoId){
        Integer likeCount = videosService.videoLike(videoId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 视频点赞取消
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity videoDisLike(@PathVariable("id")String videoId){
        Integer likeCount = videosService.videoDisLike(videoId);
        return ResponseEntity.ok(likeCount);
    }



    /**
     * 小视频评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity videoComment(@RequestParam("page") Integer page,
                                       @RequestParam("pagesize") Integer pagesize,
                                       @PathVariable("id") ObjectId videoId){
         PageResult pr = videosService.findComments(videoId,page,pagesize);
         return ResponseEntity.ok(pr);
     }

    /**
     * 发布小视频评论
     * @param videoId
     * @param map
     * @return
     */
     @PostMapping("/{id}/comments")
    public ResponseEntity publishComment(@PathVariable("id") String videoId,
                                         @RequestBody Map<String,String> map){
         String commentTxt = map.get("comment");
         //调用api
         videosService.publishComment(videoId,commentTxt);
         return ResponseEntity.ok(null);
     }

     //小视频评论点赞
     @PostMapping("/comments/{id}/like")
    public ResponseEntity videoCommentLike(@PathVariable("id") String videoCommentId){
         //调用api
         Integer count = videosService.videoCommentLike(videoCommentId);
         return ResponseEntity.ok(count);
     }

     //小视频评论点赞取消
    @PostMapping("comments/{id}/dislike")
    public ResponseEntity videoCommentDislike(@PathVariable("id") String videoCommentId){
         //调用service
        Integer count = videosService.videoCommentDislike(videoCommentId);
        return ResponseEntity.ok(count);
    }

    //视频用户关注
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long uid){
         //调用service
        Long focusCount = videosService.userFocus(uid);
        return ResponseEntity.ok(focusCount);
    }

    //视频用户取消关注
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long uid){
         //调用service
        videosService.userUnFocus(uid);
        return ResponseEntity.ok(null);
    }
}